﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "GmPickProfileCurveFirst.h"
#include "IDocument.h"
#include "IPickResult.h"
#include "IEditMode.h"
#include "IGenericElement.h"
#include "IModelLine.h"
#include "IStructureSlopeLine.h"
#include "IGraphicsNodeReference.h"
#include "IPick.h"
#include "ProfileUsedModelLineUtils.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;

void GmPickProfileCurveFirstEventHandler::Process(gcmp::IDocument* pDoc, gcmp::IPickEventArgs* pArgs, const gcmp::Vector3d& pos) const
{
    //在项目文档，轮廓编辑模式环境下，需要保证轮廓线，坡度线优先被拾取
    if (pDoc->IsFamilyDocument())
        return;

    IEditMode* pTopActiveEditMode = IEditMode::GetTopActiveEditMode(pDoc);
    if (!pTopActiveEditMode)
        return;

    OwnerPtr<IPickResult> result = pArgs->GetPickCandidates().Clone();

    //将outputGreps分组
    std::vector<OwnerPtr<IPick>> candidates_ProfileCurve;
    std::vector<OwnerPtr<IPick>> candidates_SlopeLine;
    std::vector<OwnerPtr<IPick>> candidates_Other;

    std::vector<OwnerPtr<IPick>>& candidates = result->GetAllPicks();
    FOR_EACH(candidate, candidates)
    {
        bool isProfileCurve = false;
        bool isSlopeLine = false;
        const GraphicsNodeReferenceOwnerPtrVector& gNodeReferences = candidate->GetAllGraphicsNodeReferences();
        FOR_EACH(gNodeReference, gNodeReferences)
        {
            if (IGenericElement* pGenericElement = quick_cast<IGenericElement>(pDoc->GetElement(gNodeReference->GetElementId())))
            {
                if (IModelLine* pIModelLine= quick_cast<IModelLine>(pGenericElement->GetExternalObject()))
                {
                    if (ProfileUsedModelLineUtils::IsModelLineUsedForProfile(pIModelLine))
                    {
                        isProfileCurve = true;
                    }
                }

                if (IStructureSlopeLine* pIModelLine = quick_cast<IStructureSlopeLine>(pGenericElement->GetExternalObject()))
                {
                    isSlopeLine = true;
                }
            }
        }

        if (isProfileCurve)
        {
            candidates_ProfileCurve.emplace_back(candidate->Clone());
        }
        else if (isSlopeLine)
        {
            candidates_SlopeLine.emplace_back(candidate->Clone());
        }
        else
        {
            candidates_Other.push_back(candidate->Clone());
        }
    }

    if (!candidates_ProfileCurve.empty() || !candidates_SlopeLine.empty())
    {
        candidates.clear();

        FOR_EACH(candidate_ProfileCurve, candidates_ProfileCurve)
        {
            result->AddPick(TransferOwnership(candidate_ProfileCurve));
        }
        FOR_EACH(candidate_SlopeLine, candidates_SlopeLine)
        {
            result->AddPick(TransferOwnership(candidate_SlopeLine));
        }
        FOR_EACH(candidate_Other, candidates_Other)
        {
            result->AddPick(TransferOwnership(candidate_Other));
        }
    }

    pArgs->SetPickCandidates(*result);
}
